package com.anche.cywebnew.Controller.systemManager;

import com.anche.cywebnew.Pojo.systemManager.CyUser;
import com.anche.cywebnew.Service.systemManager.CyUserService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/cyuser")
public class CyUserController {

    private static final Logger logger = LoggerFactory.getLogger(CyUserController.class);

    @Autowired
    private CyUserService cyUserService;

    @RequestMapping("/manager")
    public String manager() {
        return "systemManager/cyuser";
    }


    @RequestMapping("/cyuserPager")
    @ResponseBody
    @OptLog(title = "查询查验人员列表", type = OptType.SELECT)
    public Object cyuserPager(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, CyUser cyUser) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<CyUser> cyUsers = cyUserService.getCyuserPager(page, limit, cyUser);
            logger.info("<查询查验人员列表成功>");
            map.put("code", "0");
            map.put("count", cyUsers.getTotal());
            map.put("msg", "查询成功");
            map.put("data", cyUsers.getResult());
        } catch (Exception e) {
            logger.error("<查询查验人员列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/cyuserInfo")
    @ResponseBody
    @OptLog(title = "添加查验人员", type = OptType.ADD)
    public Object addCyuserInfo(CyUser cyUser, @RequestParam(value = "headImg_base64", required = false) String strBase64) {
        try {
            cyUserService.addCyUser(cyUser, strBase64);
            logger.info("<新增查验人员：[{}]成功>", cyUser.getCyyxm());
            return ResultUtil.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增查验人员：[{}]异常>:{}", cyUser.getCyyxm(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/cyuserInfo", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "更新查验人员", type = OptType.UPDATE)
    public Object setCyuserInfo(CyUser cyUser, @RequestParam(value = "headImg_base64", required = false) String strBase64) {
        try {
            cyUserService.setCyuserInfo(cyUser, strBase64);
            logger.info("<更新查验人员[{}]成功>", cyUser.getCyyxm());
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新查验人员[{}]异常>:{}", cyUser.getCyyxm(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }


    @RequestMapping(value = "/cyuserInfo", method = RequestMethod.DELETE)
    @ResponseBody
    @OptLog(title = "删除查验人员", type = OptType.DEL)
    public Object delCyuserInfo(@RequestParam("id") String id) {
        try {
            cyUserService.delCyuserInfo(id);
            return ResultUtil.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<删除id为[{}]的查验人员异常>:{}", id, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/cyuserList", method = RequestMethod.GET)
    @ResponseBody
    @OptLog(title = "查询查验人员", type = OptType.SELECT)
    public Object getCyuserList() {
        try {
            List<CyUser> cyUsers = cyUserService.getCyuserList();
            return ResultUtil.success(cyUsers);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<查询查验员列表异常>:{}", e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/cyuserInfo", method = RequestMethod.GET)
    @ResponseBody
    @OptLog(title = "根据cyysfzhm查询查验人员", type = OptType.SELECT)
    public Object getCyuserInfo(@RequestParam("cyysfzhm")String cyysfzhm) {
        try {
            CyUser cyUsers = cyUserService.getCyuser(cyysfzhm);
            return ResultUtil.success(cyUsers);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<查询查验员[{}]异常>:{}",cyysfzhm, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

}
